Call a block method on an iterator: each.magic.collect { ... }

Posted by blinry on Stack Overflow See other posts from Stack Overflow or by blinry
Published on 2010-04-07T09:50:33Z Indexed on 2010/04/07 9:53 UTC
Read the original article Hit count: 249

Filed under:
|
|

I have a class with a custom each-method:

class CurseArray < Array
    def each_safe
        each.do |element|
            unless element =~ "fuck"
                yield element
            end
        end
    end 
end

And want to call block methods on those "selected" elements. For example:

curse_array.each_safe.magic.collect {|element| "#{element} is a nice sentence."}

I know there is a way to do this, but I've forgotten. Please help! :-)

© Stack Overflow or respective owner

Related posts about ruby

Related posts about block